import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import {resetRouter} from '@/router'

// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests//如果这个为true 后台设置跨域 域名不能为*
  crossdomain:true,
  // baseURL: 'https://localhost:44351/',
  // baseURL: 'http://39.106.185.99:8085/',
  baseURL: 'http://www.cddreamhope.com:8085/',
  timeout: 50000 // request timeout
})
// request interceptor
service.interceptors.request.use(
  config => {
    //组装请求基础数据结构
    var requestData = {
      token: store.getters.token,
      sign: '',
      access_key: '',
      timeStemp: 0,
      body: {},
    }
    requestData.body = config.data;
  var  data= Object.assign(requestData, config.params);//params整合进入data中
    // do something before request is sent
    config.data = data;
    // config.params=undefined;//置空params
    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['X-Token'] = getToken()
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if (res.result !== 'OK') {
      Message({
        message: res.reason || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      if (res.reason === '未登录或登录已超时,请重新登录') {
        store.dispatch('user/resetToken').then(() => {
          // location.href="http://localhost:8080/"
            location.reload()
        })
      }
      // // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
      // if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
      //   // to re-login
      //   MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
      //     confirmButtonText: 'Re-Login',
      //     cancelButtonText: 'Cancel',
      //     type: 'warning'
      //   }).then(() => {
      //     store.dispatch('user/resetToken').then(() => {
      //       location.reload()
      //     })
      //   })
      // }
      return Promise.reject(new Error(res.reason || 'Error'))
    } else {
      return res
    }
  },
  error => {
    if (error.message === 'Network Error') {
      error.message = "服务器错误"
    }
    if (error.response) {
      if (error.response.status === 404) {
        error.message = error.response.data.Message
      }
      console.log(error.response)
    }
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
